package cn.jy.nvr.ftp1;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jy.nvr.mapper.ChannelMapper;
import cn.jy.nvr.mapper.DonorMapper;
@Service
public class ProcessSocketData extends Thread {
   private Socket socket;
   private static int MaxSize	= 200 * 1024 * 1024;
   public static int Socket_Buf_Size			= 5 * 1024;
   @Autowired
   ChannelMapper channelMapper;
   
   public ProcessSocketData(){
	   super();
   }
   public ProcessSocketData(Socket socket){
	   this.socket=socket;
   }
   public void run (){
	   
   try {
	   DataInputStream in =  new DataInputStream(socket.getInputStream());
	   try{
		   DataOutputStream out= new DataOutputStream(socket.getOutputStream());
		   try {
			//获取文件信息
			int length = in.readInt();
			int pos = 0;
			int count;
			byte[] filePathBytes=new byte[length];
			while (pos < length) {
				count = length - pos;
				if (count > Socket_Buf_Size) {
					count = Socket_Buf_Size;
				}
				in.read(filePathBytes, pos, count);
				pos += count;
			}
			  String str =new String(filePathBytes,"GB2312");
			  System.out.println(str);
			  //文件生成时间
			  length=in.readInt();
			  pos=0;
			  byte[] fileDateBytes=new byte[length]; 
			  while (pos < length) {
					count = length - pos;
					if (count > Socket_Buf_Size) {
						count = Socket_Buf_Size;
					}
					in.read(fileDateBytes, pos, count);
					pos += count;
				}
			  String str1 =new String(fileDateBytes,"GB2312");
			  System.out.println(str1);
			  out.writeInt(0xFC);//给客户端一个返回码  告诉服务端这边ok 若有问题就返回0xFB结束
			  out.flush();
			  length=in.readInt();
			  byte[] fileData=new byte[length];
			  
			  FileOutputStream fileout = new FileOutputStream("E://test.mp4");
			  try {
				  for (int i = 0; i < fileData.length; i++) {
					fileData[i]=in.readByte();
				}
				 fileout.write(fileData); 
				 fileout.flush();
			  }finally {
			      fileout.close();
			   }
			  //返回结果res_code和结果
			  out.writeInt(0XFC);
			  out.flush();
			  String result="成功接收";
			  byte[] resultBytes = result.getBytes("GB2312");
			    out.writeInt(resultBytes.length);
			    out.flush();
			     pos = 0;
			     length=resultBytes.length;
				while (pos < length) {
					count = length - pos;
					if (count > Socket_Buf_Size) {
						count = Socket_Buf_Size;
					}
					out.write(resultBytes, pos, count);
					out.flush();
					pos += count;
				}
				if(in.readInt()==0xFFAA)
					System.out.println("完成接收任务");
				
				
		       } catch (Exception e) {
			      e.printStackTrace();
		        }finally{
		          out.close();
		        }
		   }catch (Exception e) {
			   e.printStackTrace();
		   }finally{
			   in.close();
		    }
	      } catch (Exception e) {
		       e.printStackTrace();
	        }finally{
	        	try {
					socket.close();
				} catch (IOException e) {
				e.printStackTrace();
				}
	        }
   }
}
